//
//  UIButton+CreateButton.m
//  YiGuScience  
//
//  Created by admin on 2017/4/12.
//  Copyright © 2017年 刘艳芹. All rights reserved.
//

#import "UIButton+CreateButton.h"

@implementation UIButton (CreateButton)
static char overviewKey;

@dynamic event;

+(UIButton *)creatButtonWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font andTextColor:(UIColor *)textColor bgColor:(UIColor *)bgColor{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = font;
    [button setTitleColor:textColor forState:UIControlStateNormal];
    [button setBackgroundColor:bgColor];
    return button;
}


+(UIButton *)createButtonWithFrame:(CGRect)frame normalImage:(UIImage *)norImg highLight:(UIImage *)highImg{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = frame;
    [button setImage:norImg forState:UIControlStateNormal];
    [button setImage:highImg forState:UIControlStateSelected];
    
    return button;
}






- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block {
    objc_setAssociatedObject(self, &overviewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}


- (void)callActionBlock:(id)sender {
    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &overviewKey);
    if (block) {
        block();
    }
}
@end
